//
//  OANetPublic.m
//  LongOA
//
//  Created by 耿用强 on 15/7/5.
//  Copyright (c) 2015年 耿用强. All rights reserved.
//

#import "OANetPublic.h"
#import "OASocketMethed.h"
#import "JSON.h"

//static  Byte = 0x04;//00000100 协议标记
//public final byte INPUT_GZIP = 0x01;//00000001 压缩输入流
//public final byte OUTPUT_GZIP = 0x02;//00000010 压缩输出流
@implementation OANetPublic
+ (void)requestWithApiName:(NSString *)urlString
                                    params:(NSMutableDictionary *)params andTag:(int)tag  andMsg:(NSString*)msg andView:(UIView*)vi globalTimelinePostsWithBlock:(void (^)(NSDictionary *posts, NSError *error))block
{
    
    MBProgressHUD *hud=nil;
    if (vi!=nil) {
        for (MBProgressHUD *oldHud in  [MBProgressHUD allHUDsForView:vi]) {
            [oldHud hide:NO];
            [oldHud removeFromSuperview];
        }
        hud = [[MBProgressHUD alloc] initWithView:vi];
        if (msg.length!=0) {
            hud.labelText=msg;
        }
        [hud show:NO];
        hud.removeFromSuperViewOnHide = YES;
        if (vi!=nil) {
            [vi addSubview:hud];
        }
    }
    NSString*jsonString=nil;
    SBJsonWriter *writer = [[SBJsonWriter alloc] init];
    jsonString=[writer stringWithObject:params];
    if (jsonString.length==0) {
        jsonString=@"{}";
    }
    jsonString=[NSString stringWithFormat:@"%@%@",urlString,jsonString];
    NSData *fileData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    [[OASocketMethed sharedOASocketMethed] writeData:fileData andTag:tag back:^(NSData*data,NSError*error){
        [hud hide:YES];
        [hud removeFromSuperview];
        if (!error) {
            @try {
                NSString*dataString=@"";
                
                Byte *testByte = (Byte *)[data bytes];
                Byte testByte2[[data length]-5];
                for(int i=4;i<[data length]-1;i++)
                {
                    testByte2[i-4]=testByte[i];
                }
                NSData*datas= [[NSData alloc] initWithBytes:testByte2 length:[data length]-5];
                dataString=[[NSString alloc] initWithData:datas encoding:NSUTF8StringEncoding];
                
                NSDictionary*result=[dataString JSONValue];
                if (result==nil) {
                    if (block) {
                        NSError*error=[NSError new];
                        block(result,error);
                    }
                }
                else
                {
                    if (block) {
                        block(result,nil);
                    }
                }

            }
            @catch (NSException *exception) {
                [Commend showAlert:@"数据解析有误"];
            }
            @finally {
                
            }
        }
        else
        {
            if (block) {
                NSError*error=[NSError new];
                block(nil,error);
            }
        }
    }];
}


-(void)writeFile:(NSString*)filename and:(NSString*)data

{
    //获得应用程序沙盒的Documents目录，官方推荐数据文件保存在此
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* doc_path = [path objectAtIndex:0];
    //NSLog(@"Documents Directory:%@",doc_path);
    //创建文件管理器对象
    NSFileManager *fm = [NSFileManager defaultManager];          NSString* _filename = [doc_path stringByAppendingPathComponent:filename];
    //NSString* new_folder = [doc_path stringByAppendingPathComponent:@"test"];
    //创建目录
    //[fm createDirectoryAtPath:new_folder withIntermediateDirectories:YES attributes:nil error:nil];
    [fm createFileAtPath:_filename contents:[data dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
}

///**
// * int32转byte[]
// */
//private byte[] intToByteArray(int i) {
//    byte[] result = new byte[4];
//    result[3] = (byte) ((i >> 24) & 0xFF);
//    result[2] = (byte) ((i >> 16) & 0xFF);
//    result[1] = (byte) ((i >> 8) & 0xFF);
//    result[0] = (byte) (i & 0xFF);
//    return result;
//}


@end
